<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户ID" prop="uid">
        <el-input v-model="queryParams.uid" placeholder="请输入用户ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="应用名称" prop="appName">
        <el-select v-model="queryParams.appName" style="width: 100%;" clearable placeholder="请选择应用名称">
          <el-option v-for="(item, i) in extData.appInfoList || []" :key="i" :label="item.key" :value="item.key">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="渠道名称" prop="channelName" class="single-line-label">
        <span class="label-spacing"></span>
        <el-select v-readonlyRemove v-model="selectedChannelNames" multiple clearable placeholder="请选择渠道名称" filterable
          @change="updateChannelNames">
          <el-option v-for="(item, i) in extData.channelNameList || []" :key="i" :label="item.key"
            :value="item.key"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="queryParams.mobile" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="省市区" prop="provincial">
        <el-input v-model="queryParams.provincial" placeholder="请输入省市区" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input v-model="queryParams.age" placeholder="请输入年龄" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

<!--      <el-form-item v-for="item in dynamicParamList" :key="item.filedName" :label="item.name" :prop="item.filedName">-->
<!--        <el-select multiple style="width: 100%" v-model="form[item.filedName]" :placeholder="`请选择${item.name}`"-->
<!--          @change="handleQuery" :allow-clear="true" clearable>-->
<!--          <el-option v-for="itemOp in item.children" :key="itemOp.value" :label="itemOp.name" :value="itemOp.value">-->
<!--          </el-option>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="芝麻" prop="zhima" >
        <el-select v-model="queryParams.zhima" style="width: 100%;" clearable placeholder="请选择芝麻分">
          <el-option v-for="(item, i) in extData.zhimaMapList || []" :key="i" :label="item.key" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="贷款金额" prop="loanAmount" >
        <el-select v-model="queryParams.loanAmount" style="width: 100%;" clearable placeholder="请选择贷款金额">
          <el-option v-for="(item, i) in extData.loanAmountMapList || []" :key="i" :label="item.name" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学历" prop="xueli" >
        <el-select v-model="queryParams.xueli" style="width: 100%;" clearable placeholder="请选择学历">
          <el-option v-for="(item, i) in extData.xueliMapList || []" :key="i" :label="item.name" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="职业身份" prop="gongzuo" >
        <el-select v-model="queryParams.gongzuo" style="width: 100%;" clearable placeholder="请选择职业身份">
          <el-option v-for="(item, i) in extData.jobMapList || []" :key="i" :label="item.name" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="逾期情况" prop="yuqi" >
        <el-select v-model="queryParams.yuqi" style="width: 100%;" clearable placeholder="请选择逾期情况">
          <el-option v-for="(item, i) in extData.yuqiMapList || []" :key="i" :label="item.name" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="收入" prop="shouru" >
        <el-select v-model="queryParams.shouru" style="width: 100%;" clearable placeholder="请选择收入情况">
          <el-option v-for="(item, i) in extData.shouruMapList || []" :key="i" :label="item.name" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="贷款期限" prop="loanTime" >
        <el-select v-model="queryParams.loanTime" style="width: 100%;" clearable placeholder="请选择贷款期限">
          <el-option v-for="(item, i) in extData.loanTimeMapList || []" :key="i" :label="item.name" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="资产(多选)" prop="orFilter" >
        <el-select v-model="queryParams.orFilter" style="width: 100%;"  multiple clearable placeholder="请选择资产信息">
          <el-option v-for="(item, i) in extData.orFilterMapList || []" :key="i" :label="item.name" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="注册时间">
        <MobileDatePicker v-model="queryParams.dateRange" type="datetimerange" :default-time="['00:00:00', '23:59:59']"
          range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd HH:mm:ss" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>

      <!-- <el-form-item>
            <el-checkbox v-model="queryParams.orFlag" /> 满足任一条件
       </el-form-item> -->
    </el-form>


    <el-row :gutter="10" class="mb8">
      <el-button type="primary" plain icon="el-icon-place" size="small" @click="openCity">城市分布图</el-button>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sourceInfoList">
      <el-table-column label="用户ID" align="center" prop="uid" width="168">
        <template slot-scope="scope">
          <span class="clickable-uid" @click="handleDetailsDialog(scope.row)">{{ scope.row.uid }}</span>
        </template>
      </el-table-column>
      <el-table-column label="个人信息" align="center" width="140">
        <template slot-scope="scope">
          {{ scope.row.trealname }}--{{ scope.row.sex === 1 ? '男' : '女' }}--{{ scope.row.age }}岁
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="tmobile" width="120" />
      <el-table-column label="应用名称" align="center" prop="appName" />
      <el-table-column label="渠道名称" align="center" prop="channelName" />

      <el-table-column label="所属地" align="center" width="120">
        <template slot-scope="scope">
          {{ scope.row.province }}--{{ scope.row.city }}
        </template>
      </el-table-column>
      <el-table-column label="贷款信息" align="center" width="150">
        <template slot-scope="scope">
          <div>{{ scope.row.loanTime }} / {{ scope.row.loanAmount }}</div>
        </template>
      </el-table-column>
      <el-table-column label="房 / 车" align="center" width="120">
        <template slot-scope="scope">
          <div>{{ scope.row.house }} / {{ scope.row.car }}</div>
        </template>
      </el-table-column>
      <el-table-column label="社保 / 公积金" align="center" width="150">
        <template slot-scope="scope">
          <div>{{ scope.row.shebao }} / {{ scope.row.gjj }}</div>
        </template>
      </el-table-column>
      <el-table-column label="芝麻分 / 保险" align="center" width="150">
        <template slot-scope="scope">
          <div>{{ scope.row.zhima }} / {{ scope.row.baoxian }}</div>
        </template>
      </el-table-column>

      <el-table-column label="其他资质" align="center" width="300">
        <template slot-scope="scope">
          <div>{{ scope.row.huabei }} / {{ scope.row.baitiao }} / {{ scope.row.weili }} / {{ scope.row.shouru }} / {{
            scope.row.xueli }} / {{ scope.row.yongtu }} / {{ scope.row.yuqi }} / {{ scope.row.xinyongka }} / {{
              scope.row.gongzuo }}</div>
        </template>
      </el-table-column>

      <el-table-column label="创建时间" align="center" width="150">
        <template slot-scope="scope">
          {{ formatTimestamp(scope.row.createTime) }}
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />


    <!-- 用户详情弹窗 -->
    <Member ref="MemberRef" />

    <el-dialog :visible="open" @close="open = false" title="城市分布图" width="300%" height="500%">
  <City ref="CityRef" />
</el-dialog>


  </div>
</template>
<style scoped>
.clickable-uid {
  cursor: pointer;
  color: blue;
}
</style>
<script>
import { listSourceInfo, addSourceInfo, updateSourceInfo } from "@/api/member/sourceInfo";
import { listTaskDictInfo } from "@/api/dict/dictInfo";
import { ChannelistBaseInfo } from "@/api/channel/baseInfo";
import Member from '@/views/member/memeber.vue';
import { baseInfoExtData } from "@/api/channel/baseInfo";
import dayjs from 'dayjs';
import { isMobile } from 'mobile-device-detect';
import City from '@/views/member/city/city.vue'
export default {
  name: "SourceInfo",
  components: {
    Member,
    City
  },
  data() {
    return {
      showDialog: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: !isMobile,
      // 总条数
      total: 0,
      // 用户资质信息表格数据
      sourceInfoList: [],
      dynamicParamList: [],
      channelinfoList: [],
      selectedChannelNames: [],
      extData: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dateInfo: null,
        appCode: null,
        appName: null,
        channelCode: null,
        channelName: null,
        realname: null,
        mobile: null,
        mobileMd5: null,
        idcard: null,
        sex: null,
        age: null,
        reqIp: null,
        city: null,
        cityCode: null,
        province: null,
        provinceCode: null,
        loanAmount: null,
        loanTime: null,
        house: null,
        car: null,
        gjj: null,
        shebao: null,
        baoxian: null,
        zhima: null,
        huabei: null,
        baitiao: null,
        weili: null,
        gongzuo: null,
        yuqi: null,
        shouru: null,
        orFilter: null,
        startCreateTime: null,
        endCreateTime: null,
        dateRange: [`${dayjs().format('YYYY-MM-DD')} 00:00:00`, `${dayjs().format('YYYY-MM-DD')} 23:59:59`],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getTreeselect();
    this.initData();
    this.getOptions();
  },
  methods: {
    getTextColor(text) {
      if (text && typeof text === 'string') {
        return text.includes('无') ? 'color: red;' : 'color: darkblue;';
      }
      return 'color: darkblue;';
    },
    openCity() {
      this.open = true;
    },
    dayjs,
    getOptions() {
      baseInfoExtData().then((response) => {
        this.extData = response.data;
      });
    },
    updateChannelNames() {
      this.queryParams.channelName = this.selectedChannelNames.join(',');
    },
    /**渠道  下拉框搜索*/
    async initData() {
      const response = await ChannelistBaseInfo()
      this.channelinfoList = response.rows;
    },
    async handleDetailsDialog(row) {
      this.$refs.MemberRef.show(row.uid)
    },
    /**更改创建时间格式 */
    formatTimestamp(timestamp) {
      if (!timestamp) {
        return '';
      }
      const date = new Date(timestamp);
      const formattedDate = `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}:${date.getSeconds().toString().padStart(2, '0')}`;
      return formattedDate;
    },
    /** 查询用户资质信息列表 */
    getList() {
      this.loading = true;
      const sata = { ...this.queryParams }
      if (sata.dateRange && sata.dateRange.length === 2) {
        const startDate = this.queryParams.dateRange[0];
        const endDate = this.queryParams.dateRange[1];
        sata.startCreateTime = startDate;
        sata.endCreateTime = endDate;
      }
      if ( sata.orFilter?.length > 0){
        sata.orFilter = sata.orFilter.join(',')
      }

      delete sata.dateRange
      listSourceInfo(sata).then(response => {
        this.sourceInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        uid: null,
        dateInfo: null,
        appCode: null,
        appName: null,
        channelCode: null,
        channelName: null,
        mobile: null,
        startCreateTime: null,
        endCreateTime: null
      };
      this.resetForm("form");
    },

    handleQuery() {
      this.queryParams.pageNum = 1;
      // 遍历动态参数列表，将多选值转换为逗号分隔的字符串
      // for (const item of this.dynamicParamList) {
      //   const fieldName = item.filedName;
      //   const selectedValues = this.form[fieldName];
      //   if (Array.isArray(selectedValues)) {
      //     this.queryParams[fieldName] = selectedValues.join(',');
      //   } else {
      //     this.queryParams[fieldName] = selectedValues; // 单个值情况
      //   }
      // }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("form");
      for (const item of this.dynamicParamList) {
        this.form[item.filedName] = null;
      }
      this.queryParams.dateRange = [];
      this.queryParams.uid = null;
      this.queryParams.mobile = null;
      this.queryParams.provincial = null;
      this.queryParams.appName = null;
      this.queryParams.channelName = null;
      this.handleQuery();
    },
    /** 查询菜单下拉树结构 */
    getTreeselect() {
      listTaskDictInfo().then((response) => {
        this.dynamicParamList = this.handleTree(response.rows, "id");
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.uid)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.uid != null) {
            updateSourceInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSourceInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  }
};
</script>
